package org.example.controller;

import org.example.config.AppConfig;
import org.example.entity.User;
import org.example.properties.AppProperties;
import org.example.properties.JdbcProperties;
import org.example.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Properties;

@Controller
@RequestMapping("${mvc.front.path}")
public class IndexController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    @Value("${mvc.front.path}")
    protected String frontPath;


    @ResponseBody
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index() {
        System.out.println("success .......");

        logger.debug("debug ...");
        logger.info("info ...");
        logger.warn("warn ...");
        logger.error("error ...");

        AppConfig appConfig = AppConfig.getInstance();
        System.out.println("appConfig: " + appConfig);

        Properties properties = AppConfig.getProperties();
        System.out.println("properties: " + properties);

        System.out.println("appName: " + AppConfig.getAppName());

        System.out.println("frontPath: " + frontPath);

        System.out.println("userService: " + userService);
        System.out.println("userService: " + userService.getUser("1"));


        System.out.println("appName: " + AppProperties.appName);
        System.out.println("jdbc.driver: " + JdbcProperties.driver);

        return "ok ...";
    }

    @RequestMapping(value = "user/form")
    public String userForm(Model model) {
        User user = new User("1", "ZhangSan");
        model.addAttribute("user",user);
        return "userForm";
    }

    @RequestMapping(value = "user/save",method = RequestMethod.POST)
    public String userSave(User user,Model model) {
        model.addAttribute("user", user);
        model.addAttribute("message", ",welcome");
        return "userSaveInfo";
    }
}
